<?php
namespace Home\Controller;
use Think\Controller;

class CzsyController extends Controller {
    public function index(){
        cookie('orderid',null);
        cookie('zymd',null);
        cookie('orderidnum','Czsy',604800);
        $imei=cookie('imei');//生成唯一标识imei
        if(empty($imei)){
            $imei=date('Ymd').mt_rand(10000,99999).date('His');
            cookie('imei',$imei,31536000);
        }
        if(!empty($_REQUEST['uid'])){
            cookie('uid',$_REQUEST['uid']);
        }
        $czsycs=S('czsycs');$czsytime=cookie('czsytime');//测终生运随机数
        if(empty($czsycs)){$czsycs=mt_rand(100000,299999);S('czsycs',$czsycs);}
        if(empty($czsytime)){$czsycs=S('czsycs')+mt_rand(1,5);S('czsycs',$czsycs);cookie('czsytime',1,1600);}session('czsycs',S('czsycs'));
        $this->display("Czsy/ZM_cezhongshengyun");
    }
    public function czsy_zhifu(){
        cookie('orderidnum','Czsy',604800);
        if(cookie('zymd')==''){
            cookie('znickname',I('post.username'),604800);
            cookie('zymd',$_REQUEST['birthday'],604800);
            cookie('zhour',$_REQUEST['hour'],604800);
            cookie('zsex',$_REQUEST['gender'],604800);
        }
        if(!cookie($_REQUEST['username'])){//返回操作
            cookie('znickname',I('post.username'),604800);
            cookie('zymd',$_REQUEST['birthday'],604800);
            cookie('zhour',$_REQUEST['hour'],604800);
            cookie('zsex',$_REQUEST['gender'],604800);
        }
        if(!empty($_REQUEST['ordernum'])){//未支付订单
            $arrdata=M()->query("select * from sm_h5test where ordernum='{$_REQUEST['ordernum']}'");
            $zymd=$arrdata[0]['year'].'-'.str_pad($arrdata[0]['month'],2,"0",STR_PAD_LEFT).'-'.str_pad($arrdata[0]['day'],2,'0',STR_PAD_LEFT);
            cookie('zymd',$zymd,604800);
            cookie('zhour',$arrdata[0]['hour'],604800);
            cookie('znickname',$arrdata[0]['username'],604800);
            cookie('zsex',$arrdata[0]['sex'],604800);

            cookie('orderid',$_REQUEST['ordernum'],604800);
            cookie('orderidnum','Czsy',604800);
        }

        $y=mb_substr(cookie('zymd'),0,4);//年
        $m=mb_substr(cookie('zymd'),5,2); //月
        $d=mb_substr(cookie('zymd'),8,2);//日
        $zyinli=yinli($y,$m,$d);
        cookie('zyangli',$y.'年'.$m.'月'.$d.'日'.' '.cookie('zhour').'时',604800);//阳历
        cookie('zyinli',$zyinli[0].'年'.$zyinli[1].$zyinli[2],604800);//阴历

        $data=cookie('orderid');//获取订单号
        if(empty($data)&&empty($_REQUEST['ordernum'])){
            $orderid='ZS'.date('Ymd').mt_rand(100,999).date('His');
            cookie('orderid',$orderid,604800);
        }

        //创建订单
        $hour=cookie('zhour');
        $shop=cookie('orderid');
        $znickname=cookie('znickname');
        $zsex=cookie('zsex');
        $imei=cookie('uid');if(cookie('uid')==''){$imei=cookie('imei');}
        $y=mb_substr(cookie('zymd'),0,4);//年
        $m=mb_substr(cookie('zymd'),5,2); //月
        $d=mb_substr(cookie('zymd'),8,2);//日
        cookie('price',68,604800);//价格68
        $price=cookie('price');
//        $datastr=stristr(cookie('znickname'),'测试');
//        if($datastr){
//            $price=0.01;
//        }
        if(empty($_REQUEST['ordernum'])){
            $arrdata=M()->query("select * from sm_h5test where ordernum='{$shop}'");
            if(empty($arrdata)){
                M()->query("insert into sm_h5test (ordernum,price,username,typeid,sex,status,datetype,year,month,day,hour,paykind,date_remark,ip)values(
                                              '{$shop}','{$price}','{$znickname}',15,{$zsex},-1,0,{$y},{$m},{$d},{$hour},-1,'{$imei}','{$_SERVER['REMOTE_ADDR']}')");
                M()->query("insert into tb_user_information values(null,'{$shop}','{$_REQUEST['emotion']}','{$_REQUEST['childrens']}','{$_REQUEST['enterprise']}')");
            }
            if(!cookie($_REQUEST['username'])){//返回操作
                //更新订单
                M()->query("update sm_h5test set username='{$znickname}',year={$y},month={$m},day={$d},hour={$hour},sex={$zsex} where ordernum='{$shop}'");
                M()->query("update tb_user_information set emotion='{$_REQUEST['emotion']}',childrens='{$_REQUEST['childrens']}',enterprise='{$_REQUEST['enterprise']}' where orderid='{$shop}'");
            }
        }

        $zymd= cookie('zymd');
        if(cookie('zsex')==1){
            $zsex='男';
        }else{
            $zsex='女';
        }
        if($zymd!=''){
            zwmp($zymd,cookie('zhour'),$zsex);
            $this->assign('ny',I('globals.ny'));//纳音
            $this->assign('arryysz',I('globals.arryysz'));//四柱阴阳
            $this->assign('sizhu',I('globals.sizhu'));//Z四柱
            $ssarray=array();
            array_push($ssarray,implode(I('globals.nianzhuss')));
            array_push($ssarray,implode(I('globals.yuezhuss')));
            array_push($ssarray,implode(I('globals.shizhuss')));
            $this->assign('nianzhuss',$ssarray);//十神
            $this->assign('canggan',I('globals.canggan'));//藏干

            $this->assign('zhishen',I('globals.zhishen'));//支神
            $this->assign('sizhuny',I('globals.sizhuny'));//四柱纳音
            $this->assign('bzdishi',I('globals.bzdishi'));//八字地势
            $this->assign('wxsqs',I('globals.wxsqs'));//旺相休囚死
            $this->assign('xiys',I('globals.xiys'));//喜用神
            $this->assign('taiyuan',I('globals.taiyuan'));//胎元
            $this->assign('rikong',I('globals.rikong'));//日空
            $this->assign('dayun',I('globals.dayun'));//大运
            $this->assign('dayunx',I('globals.dayunx'));//大运x
            $this->assign('dayuny',I('globals.dayuny'));//大运y
            $this->assign('dayungz',I('globals.dayungz'));//大运干支
            $this->assign('dayunss',I('globals.dayunss'));//大运十神
            liunian(I('globals.bznianzhu'));
            $this->assign('liunian',I('globals.liunian'));//流年性格分析
            lnyuncheng($zymd,I('globals.dayun'),I('globals.dayunss'));
            $this->assign('lnyuncheng',I('globals.lnyuncheng'));//流年运程
            $this->assign('datacolor',I('globals.datacolor'));//数字颜色
            $orderid=cookie('orderid');
            $createtime=M()->query("select createtime from sm_h5test where ordernum='{$orderid}'");
            $this->assign('createtime',$createtime[0]['createtime']);//创建时间
        }
        $this->assign('sex',cookie('zsex'));
        $this->display("Czsy/ZM_cezhongshengyun_zhifu");
    }
    public function jieguoye(){
        if(empty($_REQUEST['ordernum'])){
            $y=mb_substr(cookie('zymd'),0,4);//年
            $m=mb_substr(cookie('zymd'),5,2); //月
            $d=mb_substr(cookie('zymd'),8,2);//日
            $zyinli=yinli($y,$m,$d);
            cookie('zyangli',$y.'年'.$m.'月'.$d.'日'.' '.cookie('zhour').'时',604800);//阳历
            cookie('zyinli',$zyinli[0].'年'.$zyinli[1].$zyinli[2],604800);//阴历

            $zymd= cookie('zymd');
        }else{
            $arrdata=M()->query("select * from sm_h5test where ordernum='{$_REQUEST['ordernum']}'");
            $zymd=$arrdata[0]['year'].'-'.str_pad($arrdata[0]['month'],2,"0",STR_PAD_LEFT).'-'.str_pad($arrdata[0]['day'],2,'0',STR_PAD_LEFT);
            cookie('zymd',$zymd,604800);
            cookie('zhour',$arrdata[0]['hour'],604800);
            cookie('znickname',$arrdata[0]['username'],604800);
            cookie('zsex',$arrdata[0]['sex'],604800);

            $y=mb_substr(cookie('zymd'),0,4);//年
            $m=mb_substr(cookie('zymd'),5,2); //月
            $d=mb_substr(cookie('zymd'),8,2);//日
            $zyinli=yinli($y,$m,$d);
            cookie('zyangli',$y.'年'.$m.'月'.$d.'日'.' '.cookie('zhour').'时',604800);//阳历
            cookie('zyinli',$zyinli[0].'年'.$zyinli[1].$zyinli[2],604800);//阴历

            $zymd= cookie('zymd');
            cookie('orderid',$arrdata[0]['ordernum'],604800);
        }
        if(cookie('zsex')==1){
            $zsex='男';
        }else{
            $zsex='女';
        }
        if($zymd!=''){
            zwmp($zymd,cookie('zhour'),$zsex,'zw');
            $this->assign('ny',I('globals.ny'));//纳音
            $this->assign('arryysz',I('globals.arryysz'));//四柱阴阳
            $this->assign('sizhu',I('globals.sizhu'));//Z四柱
            $ssarray=array();
            array_push($ssarray,implode(I('globals.nianzhuss')));
            array_push($ssarray,implode(I('globals.yuezhuss')));
            array_push($ssarray,implode(I('globals.shizhuss')));
            $this->assign('nianzhuss',$ssarray);//十神
            $this->assign('canggan',I('globals.canggan'));//藏干

            $this->assign('zhishen',I('globals.zhishen'));//支神
            $this->assign('sizhuny',I('globals.sizhuny'));//四柱纳音
            $this->assign('bzdishi',I('globals.bzdishi'));//八字地势
            $this->assign('wxsqs',I('globals.wxsqs'));//旺相休囚死
            $this->assign('xiys',I('globals.xiys'));//喜用神
            $this->assign('taiyuan',I('globals.taiyuan'));//胎元
            $this->assign('rikong',I('globals.rikong'));//日空
            $this->assign('dayun',I('globals.dayun'));//大运
            $this->assign('dayunx',I('globals.dayunx'));//大运x
            $this->assign('dayuny',I('globals.dayuny'));//大运y
            $this->assign('dayungz',I('globals.dayungz'));//大运干支
            $this->assign('dayunss',I('globals.dayunss'));//大运十神
            liunian(I('globals.bznianzhu'));
            $this->assign('liunian',I('globals.liunian'));//流年性格分析
            lnyuncheng($zymd,I('globals.dayun'),I('globals.dayunss'));
            $this->assign('lnyuncheng',I('globals.lnyuncheng'));//流年运程
            $this->assign('datacolor',I('globals.datacolor'));//数字颜色
            $this->assign('age',I('globals.age'));//年龄
            $orderid=cookie('orderid');
            $createtime=M()->query("select createtime from sm_h5test where ordernum='{$orderid}'");
            $this->assign('createtime',$createtime[0]['createtime']);//创建时间

            zsqk(I('globals.srg'),I('globals.arrzx'),I('globals.arrtf'),I('globals.arrjix'),I('globals.arrxiongx'));
            $this->assign('mgarrzhux',I('globals.mgarrzhux'));//自身主星状况
            hyqg(I('globals.srg'),I('globals.arrzx'),I('globals.arrtf'),I('globals.arrjix'),I('globals.arrxiongx'),I('globals.shua'));
            $this->assign('fqarrzhux',I('globals.fqarrzhux'));//婚姻主星状况
            cyzk(I('globals.srg'),I('globals.arrzx'),I('globals.arrtf'),I('globals.arrjix'),I('globals.arrxiongx'),I('globals.shua'));
            $this->assign('cbarrzhux',I('globals.cbarrzhux'));//财运主星状况
            syfz(I('globals.srg'),I('globals.arrzx'),I('globals.arrtf'),I('globals.arrjix'),I('globals.arrxiongx'),I('globals.shua'));
            $this->assign('syarrzhux',I('globals.syarrzhux'));//事业主星状况
            jkzt(I('globals.srg'),I('globals.arrzx'),I('globals.arrtf'),I('globals.arrjix'),I('globals.arrxiongx'),I('globals.shua'));
            $this->assign('jearrzhux',I('globals.jearrzhux'));//疾厄主星状况

            cezhongshengyun(I('globals.dayungz'),I('globals.dayunss'));
            $this->assign('arrlnbx',I('globals.arrlnbx'));//流年表x
            $this->assign('arrlnby',I('globals.arrlnby'));//流年表y
            $this->assign('arrczsy',I('globals.arrczsy'));//运势详解
//            dump(I('globals.age'));
//            exit;
        }
        $orderid=cookie('orderid');
        $arrpre=M()->query("select * from tb_phone_or_email where orderid='{$_REQUEST['ordernum']}'");//查询是否绑定手机或者邮箱
        if(empty($arrpre)){$arrpre=M()->query("select * from tb_phone_or_email where orderid='{$orderid}'");}
        if(empty($arrpre[0]['id'])){$predata=1;}
        $this->assign('predata',$predata);
        $arrevaluate=M()->query("select * from tb_user_evaluate where orderid='{$_REQUEST['ordernum']}'");//查询是否评价过
        if(empty($arrevaluate)){$evaluate=1;}
        $this->assign('evaluate',$evaluate);
        $this->assign('sex',cookie('zsex'));
        $this->display("Czsy/ZM_cezhongshengyun_zhifu_jieguoye");
    }
    public function pay(){//支付宝支付
        $price=cookie('price');
        header("location:http://hy.yixueqm.com/api/wappay/payczsy.php?orderid=".cookie('orderid')."&price={$price}");
    }
    public function zzhifu(){
        $data=M()->query("select status from sm_h5test where ordernum='".cookie('orderid')."'");
        $this->assign('status',$data[0]['status']);
        $this->display("Index/ZM_zzhifu");
    }
    public function paywx(){//微信支付
        $arrdata=M()->query("select * from sm_h5test where ordernum='".cookie('orderid')."' and status=-1");
        if(!empty($arrdata)){
            $orderid='ZS'.date('Ymd').mt_rand(100,999).date('His');//重新生成订单
            cookie('orderid',$orderid,604800);
            M()->query("update sm_h5test set ordernum='".cookie('orderid')."' where id='{$arrdata[0]['id']}'");
        }else{
            header('location:'.U('Index/mycesuan','',false));
            exit;
        }
        $price=cookie('price');

        $data=[
            'orderid'=>cookie('orderid'),	      //必填 订单id（唯一有效）
            'mz'=>$price,	            //必填 金额
            'uid'=>$_SERVER['REMOTE_ADDR'],		         //必填 用户id
            'spsuc'=>"http://hy.yixueqm.com/zhiming/index.php/Home/Czsy/return_urlwx?orderid=".cookie('orderid'),     //必填 支付或取消支付后的跳转地址
            'productname'=>'测终生运',       //必填 产品名称
        ];
        $json=json_encode($data,JSON_UNESCAPED_UNICODE);

        $url = "http://www.ddznzj.com:8212/yhxf/index?callbackUrl=http://hy.yixueqm.com/zhiming/index.php/Home/Czsy/return_urlwxx?orderid=".cookie('orderid');
//        http://www.ddznzj.com:8212/yhxf/index
//        http://heavent.tunnel.echomod.cn/yhxf/index
        $headers=array(
            'Content-Type:application/json;charset=utf-8',
        );
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
        curl_setopt($ch,CURLOPT_URL,$url); //设置请求地址
        curl_setopt($ch,CURLOPT_POST,true); //post请求
        curl_setopt($ch,CURLOPT_POSTFIELDS,$json);// post请求的数据
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);//不需要证书验证
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);//不直接输出到页面
        $json = curl_exec($ch);
        header("location:".$json);
        curl_close($ch);
    }

}